package com.spdg.ring.task;

import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.content.Context;
import cn.wolf.base.BaseTask;
import cn.wolf.http.BaseResp;
import cn.wolf.http.HttpCallBack;
import cn.wolf.http.HttpRequest;
import cn.wolf.tools.Base64Util;
import cn.wolf.tools.Log;
import cn.wolf.tools.StringUtil;

import com.spdg.ring.resp.ArticleResp;
import com.spdg.ring.sqlite.model.Article;
import com.spdg.ring.sqlite.service.ArticleService;

/**
 * <p>Title: 请求详细内容   </p>
 * <p>Description: 
 * </p>
 * <p>@author: Eric.wsd                </p>
 * <p>Copyright: Copyright (c) 2012    </p>
 * <p>Company: HX Co., Ltd.          </p>
 * <p>Create Time: 2012-11-30             </p>
 * <p>Update Time:                     </p>
 * <p>Updater:                         </p>
 * <p>Update Comments:                 </p>
 */
public class ArticleTask extends BaseTask<Void, Void, ArticleResp> {
	private String url;
	private String key;

	public ArticleTask(HttpCallBack<ArticleResp> iCall, Context context, String url, String key) {
		super(iCall, context);
		this.url = url;
		this.key = key;
	}

	@Override
	protected BaseResp doInBackground(Void... params) {
		HttpRequest request = new HttpRequest(ArticleResp.class);
		ArticleResp resp = new ArticleResp();
		try {
			resp = (ArticleResp) request.execute(url);
			if (resp.isSuccess()) {
				String result = resp.getHttpResult();
				if (!StringUtil.isEmpty(result)) {
					JSONObject root = new JSONObject(result);
					JSONObject child = (JSONObject) root.get("content");

					Article article = new Article();
					
					String content = Base64Util.decode2String(child.getString("content"));
					String title = Base64Util.decode2String(child.getString("title"));
					String date = Base64Util.decode2String(child.getString("date"));
					
					article.setTitle(title);
					article.setDate(date);
					article.setContent(parseLinkHref(content));
					article.setFile(key);
					article.setIsOnline(Article.IS_ONLINE_ARTICLE);

					if(!StringUtil.isEmpty(key)) {
						// 存储到缓存
						save2DB(article);
					}

					resp.setArticle(article);
				}
			}
		} catch (Exception e) {
			Log.e("请求详细页面异常！" + e);
			resp.setStatus(BaseResp.ERROR);
		}

		return resp;
	}
	
	/**
	 * 修改详细文章的a.href路径，当有target="_blank"的时候往href加前缀。
	 * @param content
	 * @return
	 */
	private static String parseLinkHref(String content) {
		if (!StringUtil.isEmpty(content)) {
			Document doc = Jsoup.parse(content);
			Elements elements = doc.select("a");
			for (Element element : elements) {
				String srcKey = element.attr("target");
				if(!StringUtil.isEmpty(srcKey)) {
					String hrefStr = element.attr("href");
					element.attr("href", "newtab:" + hrefStr);
				}
			}

			return doc.toString();
		}

		return "";
	}

	private void save2DB(final Article article) {
		new Thread() {
			public void run() {
				// 存储缓存
				ArticleService articleService = ArticleService.newInstance(mContext);
				articleService.saveOrUpdate(article);
			}
		}.start();
	}

}
